﻿Public Class frmLogin
    Dim frmCtrl As New control
    Dim frmParent As frmGlobal
    Dim dbUser As New dbUser

    Private Sub frmLogin_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        frmGlobal.tsLabelLoginAs.Text = "Please Login First"
        frmCtrl.reDesign(Me)
        frmParent = Me.MdiParent
        lockMenu()
    End Sub

    Private Sub lockMenu()
        frmGlobal.tsLogOut.Enabled = False
        frmGlobal.tsChangePassword.Enabled = False
        For Each rowModule As DataRow In frmParent.dsModule.Tables(0).Rows
            For Each itmHeader As ToolStripMenuItem In frmGlobal.MenuStrip.Items
                Dim mdl As String = rowModule("mdlMenuName").ToString()
                If (mdl = itmHeader.Name) Then
                    itmHeader.Visible = False
                    If (itmHeader.DropDownItems.Count > 0) Then
                        For Each itm As ToolStripItem In itmHeader.DropDownItems
                            itm.Visible = False
                        Next itm
                    End If
                End If
            Next itmHeader
        Next rowModule
    End Sub

    Private Sub Login()
        Dim UserID As String = dbUser.CheckLogin(txtUsername.Text, txtpassword.Text)
        If (UserID <> "") Then
            frmGlobal.tsLoginID.Text = UserID
            frmParent.LoginID = UserID
            frmGlobal.tsLabelLoginAs.Text = "Login As " + txtUsername.Text
            frmGlobal.tsLogOut.Enabled = True
            frmGlobal.tsChangePassword.Enabled = True
            '' Open Menu
            If (UserID <> "SuperAdmin") Then
                For Each row As DataRow In dbUser.GetModulePermissionList(UserID).Tables(0).Rows
                    Dim mdl As String = row("Module").ToString()
                    For Each itm As ToolStripMenuItem In frmGlobal.MenuStrip.Items
                        If (itm.Name = mdl) Then
                            itm.Visible = True
                        End If
                    Next itm
                Next row
                frmParent.dsPermission = dbUser.GetFormPermissionList(UserID)
                For Each row As DataRow In frmParent.dsPermission.Tables(0).Rows
                    Dim frm As String = row("MenuName").ToString()
                    For Each itmHeader As ToolStripMenuItem In frmGlobal.MenuStrip.Items
                        If (itmHeader.DropDownItems.Count > 0) Then
                            For Each itm As ToolStripItem In itmHeader.DropDownItems
                                If (itm.Name = frm) Then
                                    itm.Visible = True
                                End If
                            Next itm
                        End If
                    Next itmHeader
                Next row
            Else
                For Each itm As ToolStripMenuItem In frmGlobal.MenuStrip.Items
                    itm.Visible = True
                Next itm

                For Each itmHeader As ToolStripMenuItem In frmGlobal.MenuStrip.Items
                    If (itmHeader.DropDownItems.Count > 0) Then
                        For Each itm As ToolStripItem In itmHeader.DropDownItems
                            itm.Visible = True
                        Next itm
                    End If
                Next itmHeader
            End If
            Me.Close()
        Else : MessageBox.Show("Invalid Login Account")
        End If
    End Sub

    Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
        Login()
    End Sub

    Private Sub login_KeyDown(sender As Object, e As KeyEventArgs) Handles txtpassword.KeyDown, txtUsername.KeyDown
        If (e.KeyData = Keys.Enter) Then
            Login()
        End If
    End Sub
End Class